<?php
/**
 * Created by PhpStorm.
 * User: 80496
 * Date: 2019/2/15
 * Time: 17:18
 */

namespace app\admin\controller;


use app\admin\model\File;
use app\admin\model\Member;
use think\captcha\Captcha;
use think\Controller;
use think\Request;
use think\Session;
use think\Validate;
use think\Db;
class Basic extends Controller
{
    public function login(Request $res){
        if (Session::has('user')==false){
            if($res->isAjax()){
                $db=new Member();
                $data=$res->param();
                $map= new Validate([
                    ['username', 'require|alphaDash', '用户名不能为空|用户名格式只能是字母、数字、——或_'],
                    ['password', 'require', '密码不能为空'],
                    ['captcha','require|captcha','验证码不能为空|验证码不正确'],
                ]);
                if(!$map->check($data)){
                    $this->error($map->getError());
                }
                $u=$db->where(['username'=>$data['username'],'password'=>md5($data['password'])])->find();
                if(empty($u)){
                    $this->error('用户名或密码错误');
                }else{
                    Session::set("user",$u['id']);
                    Session::set("admin_cate_id",$u['admin_cate_id']);
                    $db->save(['login_time'=>time(),'login_ip'=>$res->ip()],['id'=>$u['id']]);
                    $this->success('登录成功！正在跳转','index/index');
                }
            }else{
                return view('/login');
            }
        }else{
            $this->redirect('index/index');
        }
    }
    public function logout()
    {
        Session::delete('user');
        Session::delete('admin_cate_id');
        if(Session::has('user') or Session::has('admin_cate_id')) {
            $this->error('退出失败');
        } else {
            $this->success('正在退出...','basic/login');
        }
    }
    public function captcha(){
        $c=new Captcha();
        $c->codeSet='1234567890';
        $c->fontSize=18;
        $c->useCurve=true;
        $c->imageH=45;
        $c->imageW=120;
        $c->length=4;
        $c->reset=true;
        return $c->entry();
    }
    public function upload($module='admin',$use='visphp_thumb')
    {
        if($this->request->file('file')){
            $file = $this->request->file('file');
        }else{
            $res['code']=1;
            $res['msg']='没有上传文件';
            return json($res);
        }
        $module = $this->request->has('module') ? $this->request->param('module') : $module;//模块
        $web_config = Db::name('system')->where('web','web')->find();
        $info = $file->validate(['size'=>$web_config['file_size']*1024,'ext'=>$web_config['file_type']])->rule('date')->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . $module . DS . $use);
        if($info) {
            //写入到附件表
            $data = [];
            $data['module'] = $module;
            $data['filename'] = $info->getFilename();//文件名
            $data['filepath'] = DS . 'uploads' . DS . $module . DS . $use . DS . $info->getSaveName();//文件路径
            $data['fileext'] = $info->getExtension();//文件后缀
            $data['filesize'] = $info->getSize();//文件大小
            $data['create_time'] = time();//时间
            $data['uploadip'] = $this->request->ip();//IP
            $data['user_id'] = Session::has('admin') ? Session::get('admin') : 0;
            if($data['module'] = 'admin') {
                //通过后台上传的文件直接审核通过
                $data['status'] = 1;
                $data['admin_id'] = $data['user_id'];
                $data['audit_time'] = time();
            }
            $data['use'] = $this->request->has('use') ? $this->request->param('use') : $use;//用处
            $res['id'] = Db::name('file')->insertGetId($data);
            $res['src'] = DS . 'uploads' . DS . $module . DS . $use . DS . $info->getSaveName();
            $res['code'] = 2;

            return json($res);
        } else {
            // 上传失败获取错误信息
            $this->error('上传失败：'.$file->getError());
        }
    }
}